﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Collaboris Ltd." file="MacroReport.cs">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Contains an <see cref="IList" /> of <see cref="ProgressReport" />'s.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Engine.Report
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Xml.Serialization;

    using Collaboris.Utils;

    /// <summary>
    /// Contains an <see cref="IList"/> of <see cref="IProgressReport"/>'s.
    /// </summary>
    [Serializable]
    public class MacroReport : ReportBase<ActionReport>
    {
        #region Constants and Fields

        /// <summary>
        /// Stores the Id of the job used to run the macro
        /// </summary>
        private Guid instanceId;
        
        /// <summary>
        /// The status.
        /// </summary>
        private ProcessStatus status;

        /// <summary>
        /// The user properties.
        /// </summary>
        private SerializableObjectDictionary<string> userProperties;

        #endregion

        #region Properties
        
        
        /// <summary>
        /// Gets the action reports.
        /// </summary>
        /// <value>The action reports.</value>
        [XmlElement]
        public List<ActionReport> ActionReports
        {
            get
            {
                if (this.reportList == null)
                {
                    this.reportList = new List<ActionReport>();
                }

                return this.reportList;
            }

            set
            {
                this.reportList = value;
            }
        }

        /// <summary>
        /// Contains the most "severe" status of 
        /// any Action within the MacroReport.
        /// </summary>
        /// <remarks>
        /// The Potential Status (most severe first are :)
        /// <see cref="ProcessStatus"/>.Stop
        /// <see cref="ProcessStatus"/>.Error
        /// <see cref="ProcessStatus"/>.Success
        /// </remarks>
        [XmlAttribute]
        public override ProcessStatus Status
        {
            get
            {
                if (this.ActionReports.Count > 0)
                {
                    foreach (IProgressReport report in this.ActionReports)
                    {
                        this.status = this.DetermineWorstCase(this.status, report.Status);
                    }
                }
                else
                {
                    return this.status;
                }

                return this.status;
            }

            set
            {
                this.status = value;
            }
        }

        /// <summary>
        /// Represents the Properties created by the Action at
        /// start-up.
        /// </summary>
        [XmlElement]
        public SerializableObjectDictionary<string> UserProperties
        {
            get
            {
                if (this.userProperties == null)
                {
                    this.userProperties = new SerializableObjectDictionary<string>();
                }

                return this.userProperties;
            }

            set
            {
                this.userProperties = value;
            }
        }

        /// <summary>
        /// Gets or sets the instance of the 
        /// macro that ran (see ID to get the 
        /// Template Id)
        /// </summary>
        public Guid InstanceId
        {
            get
            {
                return this.instanceId;
            }
            set
            {
                this.instanceId = value;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets the action report for the action with the given guid 
        /// </summary>
        /// <param name="actionguid">
        /// The actionguid.
        /// </param>
        /// <param name="mode">
        /// The mode.
        /// </param>
        /// <returns>
        /// </returns>
        public ActionReport GetActionReport(Guid actionguid, ProcessMode mode)
        {
            foreach (ActionReport actionReport in this.ActionReports)
            {
                if (actionReport.Id == actionguid && actionReport.Mode == mode)
                {
                    return actionReport;
                }
            }

            return null;
        }

        /// <summary>
        /// Gets a list with the names of the completed actions.
        /// </summary>
        /// <returns>
        /// List with the names of the completed actions
        /// </returns>
        public List<int> GetActionsCompleted()
        {
            var macrosToProcess = new List<int>();

            if (this.ActionReports != null)
            {
                foreach (ActionReport actionReport in this.ActionReports)
                {
                    // Add the names of all the actions that were processed in reverse order
                    if (actionReport.Status != ProcessStatus.Skip && actionReport.Status != ProcessStatus.Invalid)
                    {
                        macrosToProcess.Insert(0, actionReport.ActionIndex);
                    }
                }
            }

            return macrosToProcess;
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return SerialisationHelper.Serialize(this);
        }

        #endregion
    }
}